from Automated import models as automated_models
from basics import models as basics_models
from ProjectTool.CasesTool.execute_cases_tool import ExecuteCasesTool
from Automated import serializers as automated_serializers
from utils.base_response import BaseResponse


def test_cases(status, **kwargs):
    # status:1代表手动测试  2代表执行定时任务
    res = BaseResponse()
    select_fields = ['test_person', 'project', 'module', 'property', 'level', 'test_status', 'status', ]
    query_set = automated_models.AutomatedTask.objects.filter(**kwargs)
    if query_set.count():
        query_set = query_set.first()
        if query_set.current_status is False:
            query_set.current_status = True
            query_set.save()
            select_dict = {key: getattr(query_set, key) for key in select_fields if getattr(query_set, key)}
            cases_query_set = basics_models.Cases.objects.filter(**select_dict)
            if cases_query_set.count():
                cases_id_dict = cases_query_set.values('id')
                cases_ids = [item['id'] for item in cases_id_dict]
                cases_exe_obj = ExecuteCasesTool(cases_ids, driver_status=True)  # 执行用例的
                res_data = cases_exe_obj.run()
                data = {
                    # 'owner_user':query_set.owner_user.id if query_set.owner_user else None,
                    'owner_user': query_set.owner_user.id,
                    'task': query_set.task.id if query_set.task else None,
                    'automated_task': query_set.id,
                    'cases_sum': res_data.get('cases_sum'),
                    'cases_execute_sum': res_data.get('cases_execute_sum'),
                    'cases_failure_sum': res_data.get('cases_failure_sum'),
                    'cases_not_execute_sum': res_data.get('cases_not_execute_sum'),
                    'cases_failure_ids': str(res_data.get('cases_failure_ids')),
                    'cases_detail_sum': res_data.get('cases_detail_sum'),
                    'cases_detail_execute_sum': res_data.get('cases_detail_execute_sum'),
                    'cases_detail_failure_sum': res_data.get('cases_detail_failure_sum'),
                    'cases_detail_not_execute_sum': res_data.get('cases_detail_not_execute_sum'),
                    'cases_detail_failure_ids': str(res_data.get('cases_detail_failure_ids')),
                    'status': status,
                }
                serializer = automated_serializers.AutomatedTaskRecordSerializers(data=data)
                if serializer.is_valid():
                    serializer.save()
                    res.data = serializer.data
                    res.msg = '创建成功'
                else:
                    res.msg = '添加失败'
                    res.error = serializer.errors
                    res.data = data
                    res.status = 1
            else:
                res.status = 1
                res.msg = '未查询到用例，请确认后再次测试！'
            query_set.current_status = False
            query_set.save()
        else:
            res.status = 1
            res.msg = '自动化任务正在执行，请稍后再试！'
    else:
        res.status = 1
        res.msg = '自动化任务不存在，请添加！'

    return res
